package com.ruoyi.business.domain.enums;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单状态枚举
 * 包含常规电商订单和积分商城订单状态
 */
public enum OrderStatus {

    /**
     * 待支付 - 用户已创建订单但未支付
     */
    WAITING_PAYMENT(1, "待支付", "用户已创建订单但未支付"),

    /**
     * 已支付 - 用户已完成支付
     */
    PAID(2, "已支付", "用户已完成支付"),

    /**
     * 已发货 - 商家已发货
     */
    SHIPPED(3, "已发货", "商家已发货"),

    /**
     * 已完成 - 订单已完成（确认收货）
     */
    COMPLETED(4, "已完成", "订单已完成"),

    /**
     * 已取消 - 订单已取消
     */
    CANCELLED(5, "已取消", "订单已取消"),

    /**
     * 已退款 - 订单已退款
     */
    REFUNDED(6, "已退款", "订单已退款"),

    /**
     * 已支付审批中 - 积分商城专用：支付完成等待审批
     */
    PAYMENT_APPROVAL_PENDING(7, "已支付审批中", "积分商城：支付完成等待审批"),

    /**
     * 审批成功待发货 - 积分商城专用：审批通过等待发货
     */
    APPROVED_PENDING_SHIPMENT(8, "审批成功待发货", "积分商城：审批通过等待发货");

    private final Integer code;
    private final String name;
    private final String description;

    OrderStatus(Integer code, String name, String description) {
        this.code = code;
        this.name = name;
        this.description = description;
    }

    /**
     * 根据code获取枚举
     */
    public static OrderStatus getByCode(Integer code) {
        if (code == null) {
            return null;
        }
        for (OrderStatus status : values()) {
            if (status.getCode().equals(code)) {
                return status;
            }
        }
        throw new IllegalArgumentException("未知的订单状态码: " + code);
    }

    /**
     * 根据name获取枚举
     */
    public static OrderStatus getByName(String name) {
        if (name == null || name.trim().isEmpty()) {
            return null;
        }
        for (OrderStatus status : values()) {
            if (status.getName().equals(name.trim())) {
                return status;
            }
        }
        throw new IllegalArgumentException("未知的订单状态名称: " + name);
    }

    /**
     * 检查状态是否可取消
     */
    public boolean canCancel() {
        return this == WAITING_PAYMENT ||
            this == PAYMENT_APPROVAL_PENDING;
    }

    /**
     * 检查状态是否可支付
     */
    public boolean canPay() {
        return this == WAITING_PAYMENT;
    }

    /**
     * 检查状态是否可发货
     */
    public boolean canShip() {
        return this == PAID ||
            this == APPROVED_PENDING_SHIPMENT;
    }

    /**
     * 检查状态是否可确认收货
     */
    public boolean canComplete() {
        return this == SHIPPED;
    }

    /**
     * 检查状态是否可退款
     */
    public boolean canRefund() {
        return this == PAID ||
            this == SHIPPED ||
            this == PAYMENT_APPROVAL_PENDING ||
            this == APPROVED_PENDING_SHIPMENT;
    }

    /**
     * 检查是否为终态（不可再变更）
     */
    public boolean isFinalStatus() {
        return this == COMPLETED ||
            this == CANCELLED ||
            this == REFUNDED;
    }

    /**
     * 检查是否为进行中状态
     */
    public boolean isInProgress() {
        return !isFinalStatus();
    }

    /**
     * 检查是否为积分商城相关状态
     */
    public boolean isPointsMallStatus() {
        return this == PAYMENT_APPROVAL_PENDING ||
            this == APPROVED_PENDING_SHIPMENT;
    }

    /**
     * 检查是否为常规电商状态
     */
    public boolean isRegularEcommerceStatus() {
        return !isPointsMallStatus();
    }

    /**
     * 获取下一个可用的状态列表
     */
    public List<OrderStatus> getNextAvailableStatuses() {
        List<OrderStatus> nextStatuses = new ArrayList<>();

        switch (this) {
            case WAITING_PAYMENT:
                nextStatuses.add(PAID);
                nextStatuses.add(CANCELLED);
                break;
            case PAID:
                nextStatuses.add(SHIPPED);
                nextStatuses.add(REFUNDED);
                break;
            case SHIPPED:
                nextStatuses.add(COMPLETED);
                nextStatuses.add(REFUNDED);
                break;
            case PAYMENT_APPROVAL_PENDING:
                nextStatuses.add(APPROVED_PENDING_SHIPMENT);
                nextStatuses.add(REFUNDED);
                nextStatuses.add(CANCELLED);
                break;
            case APPROVED_PENDING_SHIPMENT:
                nextStatuses.add(SHIPPED);
                nextStatuses.add(REFUNDED);
                break;
            case COMPLETED:
            case CANCELLED:
            case REFUNDED:
                // 终态没有下一个状态
                break;
        }

        return nextStatuses;
    }

    /**
     * 验证状态转换是否合法
     */
    public static boolean isValidTransition(OrderStatus from, OrderStatus to) {
        if (from == null || to == null || from.isFinalStatus()) {
            return false;
        }
        return from.getNextAvailableStatuses().contains(to);
    }

    // Getters
    public Integer getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    @Override
    public String toString() {
        return String.format("OrderStatus{code=%d, name='%s', description='%s'}",
            code, name, description);
    }
}
